<template>
  <div class="app-container">
    <el-form size="small" inline>
      <el-form-item label="更新日期">
        <el-date-picker
          v-model="query.selectDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新对象">
        <el-select v-model="query.type" placeholder="请选择">
          <el-option label="监管端" value="0"></el-option>
          <el-option label="经销商" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="更新关键词">
        <el-input v-model="query.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="getList()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="handleReset()">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="small" plain @click="dialogVisible = true">新增</el-button>
        <help></help>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column align="center" fixed label="序号" type="index" width="50" />
      <el-table-column align="center" label="更新日期" prop="updateDate" width="150" />
      <el-table-column align="center" label="更新内容" prop="name" show-overflow-tooltip />
      <el-table-column align="center" label="更新对象" width="80">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? '监管端' : '经销商' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <help-dialog :title="title" :visible.sync="dialogVisible" @close="close">
      <el-form size="small" label-width="120px" ref="form" :model="queryParam" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="更新日期" prop="updateDate">
              <el-date-picker
                v-model="queryParam.updateDate"
                type="date"
                placeholder="选择日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="更新内容" prop="name">
              <el-input
                v-model="queryParam.name"
                placeholder="请输入"
                type="textarea"
                :rows="6"
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新对象" prop="type">
              <el-select v-model="queryParam.type" placeholder="请选择">
                <el-option label="监管端" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="queryParam.id" type="primary" @click="handleUpdate()">更 新</el-button>
        <el-button v-else type="primary" @click="handleAdd()">添 加</el-button>
      </span>
    </help-dialog>
  </div>
</template>

<script>
import updateLogApi from '@/api/system/update-log'

export default {
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      query: {
        pageNum: 1,
        pageSize: 10,
        selectDate: [],
        startTime: '',
        endTime: '',
        type: '',
        name: ''
      },
      dialogVisible: false,
      title: '更新日志添加',
      queryParam: {
        updateDate: '',
        type: '',
        name: ''
      },
      rules: {
        updateDate: [{ required: true, message: '选择日期', trigger: 'blur' }],
        type: [{ required: true, message: '请选择', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.query.startTime = this.query.selectDate[0]
      this.query.endTime = this.query.selectDate[1]

      updateLogApi.updateLogList(this.query).then((res) => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleReset() {
      this.query = {
        pageNum: 1,
        pageSize: 10,
        selectDate: [],
        startTime: '',
        endTime: '',
        type: '',
        name: ''
      }
      this.getList()
    },
    close() {
      this.title = '更新日志添加'
      this.dialogVisible = false
      this.queryParam = {
        updateDate: '',
        type: '',
        name: ''
      }
    },
    handleAdd() {
      this.$refs.form.validate((valid) => {
        updateLogApi.updateLogAdd(this.queryParam).then((res) => {
          this.$message.success('添加成功')
          this.close()
          this.getList()
        })
      })
    },
    handleEdit(row) {
      this.queryParam = {
        updateDate: row.updateDate,
        type: row.type,
        name: row.name,
        id: row.id
      }
      this.title = '更新日志修改'
      this.dialogVisible = true
    },
    handleUpdate() {
      this.$refs.form.validate((valid) => {
        updateLogApi.updateLogUpdate(this.queryParam).then((res) => {
          this.$message.success('修改成功')
          this.close()
          this.getList()
        })
      })
    },
    handleDelete(id) {
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateLogApi.updateLogDelete({ id: id }).then((res) => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
